/**
 * 过滤器
 */
import $_fn from '~plugins'
module.exports = {
    trustHtml: function ($sce) {
        return function (input) {
            return $sce.trustAsHtml(input)
        }
    },
    filPhone: function () {
        return function(num,param1){
            var storages = localStorage.getItem('userHelp')
            if(storages){
                var routeId = JSON.parse(storages).admin.roleid
                if(num){
                    if(!/^\d{11}$/.test(num)){
                        return num
                    }else if(routeId==2 && param1 == routeId){
                        return num
                    }else if(routeId==2 ||routeId==3 || routeId==4 || routeId==5 || routeId==6 || routeId==8 || routeId==9 ||routeId==12 || routeId==15 || routeId==16){
                        return num = num.replace(/(\d{3})\d{4}(\d{4})/,'$1****$2')
                    }else{
                        return num
                    }
                }
            }
        }
    },
    filCar:function () {
        return function(num,param1){
            var storages = localStorage.getItem('userHelp')
            if(storages){
                var routeId = JSON.parse(storages).admin.roleid
                //console.log(routeId)
                if(num){
                    if(num.length < 7){
                        return num
                    }else if(routeId==2 && param1 == routeId){
                        return num
                    }else if(routeId==2 || routeId==3 || routeId==4 || routeId==5 || routeId==6 || routeId==8 || routeId==9 ||routeId==12 || routeId==15 || routeId==16){
                        return num = num.replace(/^([\u4e00-\u9fa5]{1})([A-Z]{1})[a-zA-Z\d]{5}$/,'$1$2*****')
                    }else{
                        return num
                    }
                }
            }
        }
    },
    toAll:function () {
        return function(num,param1){
            var storages = localStorage.getItem('userHelp')
            if(storages){
                var routeId = JSON.parse(storages).admin.roleid
                if(num){
                    if(routeId==2 && param1 == routeId){
                        return num
                    }else if(routeId==2 || routeId==3 || routeId==4 || routeId==5 || routeId==6 || routeId==8 || routeId==9 ||routeId==12 || routeId==15 || routeId==16){
                        return num = num.replace(/^[a-zA-Z\d\-]+$/,'*****')
                    }else{
                        return num
                    }
                }
            }
        }
    },
    toDate:function () {
        return function(date){
            if(date.length >= 10) return date.substr(0,10)
        }
    },
    toRMB:function () {
        return function(val){
           return $_fn.toRMB(val)
        }
    },
    chinese:function () {
        return function (input) {
            var numberValue = new String(Math.round(input * 100));
            var chineseValue = '';
            var String1 = '零壹贰叁肆伍陆柒捌玖';
            var String2 = '万仟佰拾亿仟佰拾万仟佰拾元角分';
            var len = numberValue.length;
            var Ch1;
            var Ch2;
            var nZero = 0;
            var String3;
            if (len > 15) {
                alert('超出计算范围');
                return '';
            }
            if (numberValue == 0) {
                chineseValue = '零元整';
                return chineseValue;
            }
            String2 = String2.substr(String2.length - len, len);
            for (var i = 0; i < len; i++) {
                String3 = parseInt(numberValue.substr(i, 1), 10);
                if (i != (len - 3) && i != (len - 7) && i != (len - 11) && i != (len - 15)) {
                    if (String3 == 0) {
                        Ch1 = '';
                        Ch2 = '';
                        nZero = nZero + 1;
                    }
                    else if (String3 != 0 && nZero != 0) {
                        Ch1 = '零' + String1.substr(String3, 1);
                        Ch2 = String2.substr(i, 1);
                        nZero = 0;
                    }
                    else {
                        Ch1 = String1.substr(String3, 1);
                        Ch2 = String2.substr(i, 1);
                        nZero = 0;
                    }
                }
                else {
                    if (String3 != 0 && nZero != 0) {
                        Ch1 = '零' + String1.substr(String3, 1);
                        Ch2 = String2.substr(i, 1);
                        nZero = 0;
                    }
                    else if (String3 != 0 && nZero == 0) {
                        Ch1 = String1.substr(String3, 1);
                        Ch2 = String2.substr(i, 1);
                        nZero = 0;
                    }
                    else if (String3 == 0 && nZero >= 3) {
                        Ch1 = '';
                        Ch2 = '';
                        nZero = nZero + 1;
                    }
                    else {
                        Ch1 = '';
                        Ch2 = String2.substr(i, 1);
                        nZero = nZero + 1;
                    }
                    if (i == (len - 11) || i == (len - 3)) Ch2 = String2.substr(i, 1);
                }
                chineseValue = chineseValue + Ch1 + Ch2;
            }
            if (String3 == 0) chineseValue = chineseValue + '整';
            return chineseValue;
        }
    }
}
